package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import ModelLayer.DiscountGroup;

public class DBDiscountGroup implements IFDBDiscountGroup {
	private Connection con;

	public DBDiscountGroup() {
		con = DBConnection.getInstance().getDBcon();
	}

	@Override
	public DiscountGroup getDiscount(int groupId) {
		String wClause = "  groupId = '" + groupId + "'";
		return singleWhere(wClause);
	}

	@Override
	public int insertDiscount(DiscountGroup group) {
		int nextId = GetMaxId
				.getMaxId("Select max(groupId) From discountgroup") + 1;
		System.out.println("next groupId = " + nextId);
		int rc = -1;
		String query = "INSERT INTO discountgroup(groupId, name, discount) VALUES('"
				+ nextId
				+ "','"
				+ group.getName()
				+ "','"
				+ group.getDiscount() + "')";

		System.out.println("insert : " + query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
			con.commit();
		} catch (SQLException ex) {
			System.out.println("Discount is not inserted");
		}
		return rc;
	}

	@Override
	public ArrayList<DiscountGroup> getAllDiscount() {
		return miscWhere("");
	}

	@Override
	public int updateDiscount(DiscountGroup group) {
		DiscountGroup groupObj = group;
		int rc = -1;
		String query = "UPDATE discountgroup SET " + "name = '"
				+ groupObj.getName() + "'," + "discount = '"
				+ groupObj.getDiscount() + "'" + " WHERE groupId = '"
				+ groupObj.getId() + "'";
		System.out.println("Update query:" + query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (Exception e) {
			System.out.println("Update exception in discountgroup db: " + e);
		}
		return rc;
	}

	@Override
	public int deleteDiscount(int groupId) {
		int rc = -1;

		String query = "DELETE FROM discountgroup WHERE groupId = '" + groupId
				+ "'";
		System.out.println(query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (Exception ex) {
			System.out.println("Delete exception in discountgroup db: " + ex);
		}
		return (rc);
	}

	private DiscountGroup singleWhere(String wClause) {
		ResultSet results;

		DiscountGroup dg = new DiscountGroup();
		String query = buildQuery(wClause);
		System.out.println(query);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);
			if (results.next()) {
				dg = buildDiscountGroup(results);
				stmt.close();

			}
		} catch (Exception e) {
			System.out.println("Query exception: " + e);
		}
		return dg;
	}

	private ArrayList<DiscountGroup> miscWhere(String wClause) {
		ResultSet results;
		ArrayList<DiscountGroup> list = new ArrayList<DiscountGroup>();
		String query = buildQuery(wClause);
		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			while (results.next()) {
				DiscountGroup dg = new DiscountGroup();
				dg = buildDiscountGroup(results);
				list.add(dg);
			}
			stmt.close();

		} catch (Exception e) {
			System.out.println("Query exception - select: " + e);
			e.printStackTrace();
		}
		return list;
	}

	private DiscountGroup buildDiscountGroup(ResultSet results) {
		DiscountGroup dg = new DiscountGroup();
		try {
			dg.setId((results.getInt("groupId")));
			dg.setDiscount((results.getDouble("discount")));
			dg.setName((results.getString("name")));

		} catch (Exception e) {
			System.out.println("error in building the discountgroup object");
		}
		return dg;
	}

	private String buildQuery(String wClause) {
		String query = "SELECT groupId, name, discount FROM discountgroup";
		if (wClause.length() > 0) {
			query = query + " WHERE" + wClause;
		}
		return query;
	}

}
